//index.js
//获取应用实例
var route = require('../../utils/route');
var config = require('../../config/config');
var httpClient = require('../../utils/httpClient');
var common = require('../../utils/common');
var enum_type = require('../../utils/enum');
var util = require('../../utils/util');

Page({
  data: {
    imgUrls: [
      '/image/banner.png',
      '/image/banner.png',
      '/image/banner.png'
    ],
    items:[{
      url:'/image/list_pic.png',
      label: '/image/icon5.png',
      bigTxt:'无双永恒★5.8',
      smallTxt: '今日开放，最新版本，最新地图'
    },
      {
        url: '/image/list_pic.png',
        label:'/image/icon5.png',
        bigTxt: '无双永恒★5.8',
        smallTxt: '今日开放，最新版本，最新地图'
      },
      {
        url: '/image/list_pic.png',
        label: '/image/icon5.png',
        bigTxt: '无双永恒★5.8',
        smallTxt: '今日开放，最新版本，最新地图'
      },
      {
        url: '/image/list_pic.png',
        label: '/image/icon5.png',
        bigTxt: '无双永恒★5.8',
        smallTxt: '今日开放，最新版本，最新地图'
      },
    ],
    product: [
      {
        urlP: '/image/icon2.png',
        label: '/image/icon5.png',
        name:'音乐猜歌',
        cont:'这些歌你真的都听过吗？中华小曲库！',
        visit:'100000',
        btn:'进入'
      },
      {
        urlP: '/image/icon2.png',
        name: '音乐猜歌',
        cont: '这些歌你真的都听过吗？中华小曲库！',
        visit: '100000',
        btn: '进入'
      },
    ],
    icons:[{

    }]
  }, loadClickData: function (options){
    var page = util.getCurrentPageUrl();
    var dataPrams = {
      type: enum_type.pageTypeEnum.INDEX,
      page: page
    };
    var source = options.source;
    if (null==source){
      dataPrams.jumpType = enum_type.jumpTypeEnum.DEFAULT;
    }else{
      dataPrams.source = source;
      dataPrams.jumpType = enum_type.jumpTypeEnum.COMING;
    }
    common.saveClickData(dataPrams);
  },
  onLoad: function (options) {

    this.loadClickData(options);

    var that = this;
    var method = route.getIndex();
    var params = {
      appid: config.appId,
    };
    
    httpClient.request(method, 'GET', params, function (res, err) {
      console.log(res);
      if (res.pageType == 2) {
        wx.setNavigationBarTitle({
          title: res.title//页面标题为路由参数
        })
        that.getMainData();
        if (res.defaultJumpType==1){
          if (res.jumpType==1){
            that.navigateToH5(res.h5Url);
            that.jumpClickData(enum_type.pageTypeEnum.INDEX,res.id);
          }
          else if (res.jumpType == 2) {
            that.navigateToMiniProgram(res.jumpAppId, res.appUrl);
            that.jumpClickData(enum_type.pageTypeEnum.INDEX, res.id);
          }
        }
      } else if (res.pageType == 1){
        wx.redirectTo({
          url: '../singlePage/singlePage?title=' + res.title,
        })
      }
    });

  },getMainData:function(){
    var that = this;
    var method = route.getMainPage();
    var params = {
      appid: config.appId,
    };
    httpClient.request(method, 'GET', params, function (res, err) {
      that.setData({
        imgUrls: res.banners,
        items: res.tableLists,
        product: res.businesses,
        icons: res.navLists
      })

    });
  },
  jumpClickData: function (type, dataId) {
    var page = util.getCurrentPageUrl();
    var dataPrams = {
      type: type,
      page: page,
      dataId:dataId,
      jumpType: enum_type.jumpTypeEnum.OUT
    };

    common.saveClickData(dataPrams);
  },

  onShareAppMessage: function (options) {

  },
  jumpPage: function (event) {
    
    var item = event.currentTarget.dataset.item;
    var type = event.currentTarget.dataset.type;
    var dataId = item.id;
    this.jumpClickData(type,dataId);

    if(item.jumpType==1){
      var details=item.detail;
      wx.setStorageSync('details', details);
      this.navigateToDetail();
    } else if (item.jumpType == 2){
      this.navigateToMiniProgram(item.appId,item.appPage);
    } else if (item.jumpType == 3){
      this.navigateToH5(item.h5Url);
    }
  },
  navigateToH5: function (h5Url){
    wx.navigateTo({
      url: '../webview/webview?h5Url=' + h5Url,
    })
  },navigateToDetail: function () {
    wx.navigateTo({
      url: '../detail/detail',
    })
  }, navigateToMiniProgram: function (appid,page) {
    wx.navigateToMiniProgram({
      appId: appid,
      path: page,
      envVersion: 'trial',
      success(res) {
        // 打开成功
        console.log(res);
      }
    })
  }
})
